home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / mail / transpor / ifmail23.z / ifmail23 / ifmail / ifgate / getmessage.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-04  |  5.0 KB  |  227 lines

  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <time.h>
  8. #include "xutil.h"
  9. #include "lutil.h"
  10. #include "bread.h"
  11. #include "ftn.h"
  12. #include "rfcmsg.h"
  13. #include "mkrfcmsg.h"
  14. #include "config.h"
  15.  
  16. extern time_t parsedate(char *,void *);
  17. extern char *rfcdate(time_t);
  18.  
  19. static time_t parsefdate(char *,void *);
  20. static time_t parsefdate(str,now)
  21. char *str;
  22. void *now;
  23. {
  24.     if ((strncasecmp(str,"Sun ",4) == 0) ||
  25.         (strncasecmp(str,"Mon ",4) == 0) ||
  26.         (strncasecmp(str,"Tue ",4) == 0) ||
  27.         (strncasecmp(str,"Wed ",4) == 0) ||
  28.         (strncasecmp(str,"Thu ",4) == 0) ||
  29.         (strncasecmp(str,"Fri ",4) == 0) ||
  30.         (strncasecmp(str,"Sat ",4) == 0))
  31.         str+=4;
  32.     return parsedate(str,now);
  33. }
  34.  
  35. /* 0-no more messages, 1-more messages, 2-bad file */
  36. int getmessage(pkt,p_from,p_to)
  37. FILE *pkt;
  38. faddr *p_from,*p_to;
  39. {
  40.     char buf[BUFSIZ];
  41.     char *subj=NULL,*orig=NULL;
  42.     char *p,*l,*r;
  43.     rfcmsg *kmsg=NULL,**tmsg;
  44.     int tmp;
  45.     faddr f,t,*o;
  46.     int flags;
  47.     int waskluge,badkludge;
  48.     time_t mdate=0L;
  49.     off_t endmsg_off,tear_off,orig_off,via_off;
  50.     FILE *fp;
  51.  
  52.     tmsg=&kmsg;
  53.  
  54.     switch(tmp=iread(pkt))
  55.     {
  56.     case 0:    debug(5,"zero message type - end of packet?");
  57.         tmp=iread(pkt);
  58.         if (feof(pkt)) return 0;
  59.         else
  60.         {
  61.             loginf("Junk after the logical end of packet at %lu skipped",
  62.                 ftell(pkt));
  63.             return 0;
  64.         }
  65.     case 2:    break;
  66.     default:logerr("bad message type: 0x%04x",tmp);
  67.         return 2;
  68.     }
  69.  
  70.     f.zone=0;
  71.     t.zone=0;
  72.     f.point=0;
  73.     t.point=0;
  74.     f.name=NULL;
  75.     t.name=NULL;
  76.     f.domain=NULL;
  77.     t.domain=NULL;
  78.  
  79.     f.node=iread(pkt);
  80.     t.node=iread(pkt);
  81.     f.net=iread(pkt);
  82.     t.net=iread(pkt);
  83.     flags=iread(pkt);
  84.     tmp=iread(pkt);
  85.     if (aread(buf,sizeof(buf)-1,pkt))
  86.         mdate=parsefdate(buf,NULL);
  87.     if (aread(buf,sizeof(buf)-1,pkt)) logerr("date not null-terminated");
  88.     if (aread(buf,sizeof(buf)-1,pkt))
  89.         t.name=xstrcpy(buf);
  90.     if (aread(buf,sizeof(buf)-1,pkt)) logerr("toname not null-terminated");
  91.     if (aread(buf,sizeof(buf)-1,pkt))
  92.         f.name=xstrcpy(buf);
  93.     if (aread(buf,sizeof(buf)-1,pkt)) logerr("fromname not null-terminated");
  94.     if (aread(buf,sizeof(buf)-1,pkt))
  95.         subj=xstrcpy(buf);
  96.     if (aread(buf,sizeof(buf)-1,pkt)) logerr("subj not null-terminated");
  97.  
  98.     f.zone=p_from->zone;
  99.     t.zone=p_to->zone;
  100.  
  101.     debug(5,"message from %s",ascfnode(&f,0x7f));
  102.     debug(5,"message to   %s",ascfnode(&t,0x7f));
  103.     debug(5,"message subj \"%s\"",subj);
  104.     debug(5,"message date \"%s\"",rfcdate(mdate));
  105.  
  106.     tear_off=0L;
  107.     orig_off=0L;
  108.     via_off=0L;
  109.     waskluge=0;
  110.     if ((fp=tmpfile()) == NULL)
  111.     {
  112.         logerr("$unable to open temporary file");
  113.         return 3;
  114.     }
  115.     while (aread(buf,sizeof(buf)-1,pkt))
  116.     if ((buf[0] == '\1') ||
  117.         !strncmp(buf,"AREA:",5) ||
  118.         !strncmp(buf,"SEEN-BY",7)) /* This is a kluge line */
  119.     {
  120.         waskluge=1;
  121.         badkludge=0;
  122.         if (buf[0] == '\1') l=buf+1;
  123.         else l=buf;
  124.         if (*l == '\n') badkludge=1;
  125.         else while (isspace(*l)) l++;
  126.         for (p=l;*p;p++)
  127.             if ((*p != '\n') && !isprint(*p)) badkludge=1;
  128.         p=strchr(l,':');
  129.         r=strchr(l,' ');
  130.         if (p && (!r || (r > p))) r=p;
  131.         if (r == NULL) badkludge=1;
  132.  
  133.         *tmsg=(rfcmsg *)xmalloc(sizeof(rfcmsg));
  134.         (*tmsg)->next=NULL;
  135.         if (badkludge)
  136.         {
  137.             (*tmsg)->key=xstrcpy("KLUDGE");
  138.             p=printable(l,0);
  139.             r=p+strlen(p)-2;
  140.             if (strcmp(r,"\\n") == 0)
  141.             {
  142.                 *r++='\n';
  143.                 *r='\0';
  144.             }
  145.             (*tmsg)->val=xstrcpy(p);
  146.         }
  147.         else
  148.         {
  149.             *r++='\0';
  150.             while (isspace(*r)) r++;
  151.             (*tmsg)->key=xstrcpy(l);
  152.             (*tmsg)->val=xstrcpy(r);
  153.         }
  154.         tmsg=&((*tmsg)->next);
  155.  
  156.         if (!strcmp(l,"Via") && (via_off == 0L))
  157.         { 
  158.             via_off=ftell(fp);
  159.             debug(5,"^AVia \"%s\" at offset %ld",buf,via_off);
  160.         }
  161.     }
  162.     else /* this is not a kludge line */
  163.     {
  164.         if (waskluge && (isspace(buf[0]))) 
  165.             fputs("\n",fp); /* first body line is not RFC hdr */
  166.         waskluge=0;
  167.         if (!strncmp(buf,"---",3) && (buf[3] != '-'))
  168.         {
  169.             tear_off=ftell(fp);
  170.             debug(5,"tearline \"%s\" at offset %ld",buf,tear_off);
  171.         }
  172.         else if (!strncmp(buf," * Origin:",10))
  173.         {
  174.             orig_off=ftell(fp);
  175.             debug(5,"origin \"%s\" at offset %ld",buf,orig_off);
  176.             p=buf+10;
  177.             while (*p == ' ') p++;
  178.             if ((l=strrchr(p,'(')) && (r=strrchr(p,')')) &&
  179.                 (l < r))
  180.             {
  181.                 *l='\0';
  182.                 *r='\0';
  183.                 l++;
  184.                 if ((o=parsefnode(l)))
  185.                 {
  186.                     f.point=o->point;
  187.                     f.node=o->node;
  188.                     f.net=o->net;
  189.                     f.zone=o->zone;
  190.                     if (o->domain) f.domain=o->domain;
  191.                     o->domain=NULL;
  192.                     tidy_faddr(o);
  193.                     debug(5,"Origin from: %s",
  194.                         ascfnode(&f,0x7f));
  195.                 }
  196.             }
  197.             else if (*(l=p+strlen(p)-1) == '\n') *l='\0';
  198.             for (l=p+strlen(p)-1;*l == ' ';l--) *l='\0';
  199.             orig=xstrcpy(p);
  200.         }
  201.         fputs(buf,fp);
  202.     }
  203.  
  204.     endmsg_off=ftell(fp);
  205.     if ((tear_off) && (tear_off < endmsg_off)) endmsg_off=tear_off;
  206.     if ((orig_off) && (orig_off < endmsg_off)) endmsg_off=orig_off;
  207.     if ((via_off) && (via_off < endmsg_off)) endmsg_off=via_off;
  208.     debug(5,"end message offset %ld",endmsg_off);
  209.     
  210.     rewind(fp);
  211.     mkrfcmsg(&f,&t,subj,orig,mdate,flags,fp,endmsg_off,kmsg);
  212.  
  213.     fclose(fp);
  214.     tidyrfc(kmsg);
  215.     if(f.name) free(f.name); f.name=NULL;
  216.     if(t.name) free(t.name); t.name=NULL;
  217.     if(f.domain) free(f.domain); f.domain=NULL;
  218.     if(t.domain) free(t.domain); t.domain=NULL;
  219.  
  220.     if (feof(pkt) || ferror(pkt))
  221.     {
  222.         logerr("Unexpected end of packet");
  223.         return 2;
  224.     }
  225.     return 1;
  226. }
  227.